$(function() {
	$('.step1').click(function() {
		var flag=false;
		var phone = $('#accountmodel-username').val();
		var captcha = $('#accountmodel-captcha').val();
		if(!phone) {
			layer.msg('请输入手机号');
			return false;
		}
		
		if(!checkMobile(phone)) {
			layer.msg('请输入正确的手机号');
			return false;
		} else {
			$.get('/user/register_validate', {
				mobile: phone
			}, function(result) {
				if(result.code == 0) {
					layer.msg('该账号未注册')
					return false;
				}else{
					if(!captcha) {
						layer.msg('请输入验证码');
						return false;
					}else{
						$.post('/user/forget_pwd', {
							mobile: phone,
							captcha: captcha
						}, function(res) {
							if(res.code == 0) {
								layer.msg(res.message);
								$("#vcode").trigger("click"); 
								return false;
							}else{
								window.location = "/user/forget_pwd/step/2.html";
							}
						}, "json");
					}
				}
			}, "json");
		}
		

	})
	var flag=true;
	$('#get_phonecode').click(function(){
		if(flag){
			flag=false;
			$.post('/user/forget_pwd_moblie', {
			type: 1
			}, function(res) {
				if(res.code == 0) {
					layer.msg(res.message);
					setTimeout('window.location = "/user/forget_pwd/step/1.html";',1000);
					
					return false;
				}else{
					var second = 60;
					var timer = null;
					timer = setInterval(function(){
						second -= 1;
						if(second >0 ){
							$(this).attr("disabled", "true");
							$('.phonecode').html(second+'s后重发');
						}else{
							flag=true;
							clearInterval(timer);
							second=60;
							$('.phonecode').html('获取手机验证码');
							 $(this).attr("disabled", "false");
						}
					},1000);
					layer.msg(res.message);
				}
			}, "json");
		}
		
	})
	
	$('.step2').click(function(){
		var sms_captcha=$('#accountmodel-sms_captcha').val();
		if(sms_captcha){
			$.post('/user/forget_pwd_yanzheng', {
				sms_captcha: sms_captcha
			}, function(res) {
				if(res.code == 0) {
					layer.msg(res.message);
				}
				if(res.code == 1) {
					layer.msg(res.message);
					setTimeout('window.location = "/user/forget_pwd/step/3.html";',1000);
				}
				if(res.code == 2) {
					layer.msg(res.message);
					setTimeout('window.location = "/user/forget_pwd/step/1.html";',1000);
				}
			}, "json");
		}else{
			layer.msg('请输入手机验证码');
		}
	})
	
	/**
		 * 密码框上眼睛的控制
		 */
	
		$('.pwd-toggle').click(function(){
			var id = $(this).data("id");
			if ($('.pwd-toggle').hasClass('fa-eye')) {
				$('.pwd-toggle').removeClass('fa-eye');
				$('.pwd-toggle').addClass('fa-eye-slash');
				$('#' + id).attr("type", "password");
			} else {
				$('.pwd-toggle').addClass('fa-eye');
				$('.pwd-toggle').removeClass('fa-eye-slash');
				$('#' + id).attr("type", "text");
			}
		});
		
		$("#pwdInput").bind('input propertychange',function(){
   			var pwd=$("#pwdInput").val();
   			var pass=/^[^\u4e00-\u9fa5\·]{6,20}$/;
   			if(pass.test(pwd)){    
   				console.log('ok'); 
   				$(this).removeClass('error');  
   				$('#pwdInput-error').hide();
   			}else{         
   				$(this).addClass('error');  
   				$('#pwdInput-error').show();
   			}
		});
		
		$('.step3').click(function(){
			var pwd=$("#pwdInput").val();
			var pass=/^[^\u4e00-\u9fa5\·]{6,20}$/;
			if(pass.test(pwd)){    
	   			$.post('/user/forget_pwd_rest', {
					pwd: pwd
				}, function(res) {
					if(res.code == 0) {
						layer.msg(res.message);
					}
					if(res.code == 1) {
						layer.msg(res.message);
						setTimeout('window.location = "/user/forget_pwd/step/4.html";',1000);
					}
				}, "json");
   			}else{         
   				layer.msg('请注意密码格式');
   			}
		})
	
  
})

/**
 * 手机号码格式判断
 * @param tel
 * @returns {boolean}
 */
function checkMobile(tel) {
	var reg = /(^1[3|4|5|7|8][0-9]{9}$)/;
	if(reg.test(tel)) {
		return true;
	} else {
		return false;
	};
}